package com.xur.springboot.service.impl;

import com.xur.springboot.entity.User;
import com.xur.springboot.entity.internship;
import com.xur.springboot.mapper.UserMapper;
import com.xur.springboot.mapper.detailsMapper;
import com.xur.springboot.mapper.internshipMapper;
import com.xur.springboot.service.InternshipService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class InternshipServiceImpl implements InternshipService {

    @Autowired
    private internshipMapper internshipMapper;
    @Autowired
    private detailsMapper detailsMapper; // details 表操作

    //查询所有职位信息
    @Override
    public List<internship> queryAllInternship() {
        return this.internshipMapper.queryAllInternship();
    }

    //根据职位性质（兼职、全职、实习）查询职位信息
    @Override
    public List<internship> queryBossInfoByRemark(String remark) {
        return this.internshipMapper.queryBossInfoByRemark(remark);
    }

    //根据id查询职位信息
    @Override
    public List<internship> queryBossInfoById(int id) {
        return this.internshipMapper.queryBossInfoById(id);
    }

    @Override
    @Transactional // 开启事务管理
    public boolean createInternshipWithDetail(internship entity, String detailContent) {
        try {
            // 1. 插入主表（internship）
            int result = internshipMapper.insertInternship(entity);
            if (result <= 0) {
                return false; // 插入失败
            }

            // 2. 获取主表自增ID
            int generatedId = entity.getId();

            // 3. 插入详情表（details）
            int detailResult = detailsMapper.insertDetail(detailContent, generatedId);
            return detailResult > 0;
        } catch (Exception e) {
            // 自动回滚事务
            throw new RuntimeException("发布失败: " + e.getMessage());
        }
    }

    @Override
    public List<internship> queryByUserDetailIds(List<Integer> ids) {
        return internshipMapper.queryByUserDetailIds(ids);
    }
}